package edu.gatech.asthma.activity;

import java.util.ArrayList;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.database.SymptomRecordVO;
import edu.gatech.asthma.ui_support.HelpMenuHelper;
import edu.gatech.asthma.ui_support.ProfileCustomizer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Activity that displays all symptoms records
 */
public class ShowSymptomRecordsActivity extends Activity {
	int identifier = -1;
	boolean younger = false;
	DBAdapter database;
	ProfileCustomizer customizer;
	ArrayList<SymptomRecordVO> records;
	ArrayList<String> recordTimes;
	ArrayList<Integer> recordIDs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			identifier = extras.getInt("ID");
			younger = extras.getBoolean("Age");
		}

		getActionBar().setTitle("Symptom Records");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_show_symptom_records);

		database = new DBAdapter(this);
		database.open();
		customizer = new ProfileCustomizer(this,this.findViewById(android.R.id.content),database,identifier);
		customizer.setCustomizations();

		final ListView listview = (ListView)findViewById(R.id.SymptomRecordsListView);
		records = database.getSymptomRecords(identifier);
		recordTimes = new ArrayList<String>();
		recordIDs = new ArrayList<Integer>();

		if(!records.isEmpty()) {
			for(SymptomRecordVO r : records){
				recordTimes.add(r.getDate());
				recordIDs.add(r.getId());
			}
			ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, recordTimes);
			listview.setAdapter(adapter);

			listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
					Intent i;
					i = new Intent(ShowSymptomRecordsActivity.this, ShowSymptomRecordDetailActivity.class);
					i.putExtra("RecordID", recordIDs.get(position));
					i.putExtra("ID", identifier);
					i.putExtra("Age", younger);
					startActivity(i);
				}
			});
		}
	}
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}
}
